/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;
import DTO.CFields;
import DTO.Project_manager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Project_managerBUS extends IData<Project_manager>{

    @Override
    public Project_manager add(Project_manager item) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected void add(Project_manager item, int t) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void addrows(List<Project_manager> list, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Project_manager Find(Project_manager item, List<Project_manager> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    /*
        Minh
        Add body
    */
    @Override
    protected Object OnSubmit(Project_manager item, int t, SubConfig config) {
        //Object rs = 0;
        //return rs;
        Object result = 0;
        String mysql ="";
        PreparedStatement cs;      
        try 
        {
            if(t == 0)
            {
                mysql = "insert into " + CFields.tb_project_manager + "(" + CFields.project_name
                        + "," + CFields.description + "," + CFields.user_id + ","
                        + CFields.user_ids + "," + CFields.user_is_remove + ","
                        + CFields.create_date + "," + CFields.begin_date + ","
                        + CFields.end_date + "," + CFields.finish_date + ","
                        + CFields.path + "," + CFields.isdelete 
                        +  ") " 
                        + " values(?,?,?,?,?,?,?,?,?,?,?)";                
                cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);  
                cs.setString(1, item.getProject_name());
                cs.setString(2, item.getDescription());
                cs.setLong(3, item.getUser_id());
                cs.setString(4, item.getUser_ids());
                cs.setString(5, item.getUser_is_remove());
                cs.setTimestamp(6,item.getCreate_date());
                cs.setTimestamp(7,item.getBegin_date());
                cs.setTimestamp(8,item.getEnd_date());
                cs.setTimestamp(9,item.getFinish_date());
                cs.setString(10, item.getPaths());
                cs.setBoolean(11, false);
                cs.executeUpdate();
                ResultSet keyResultSet = cs.getGeneratedKeys();
                if (keyResultSet.next()) {
                    result = (long) keyResultSet.getInt(1);      
                }
            }
            else if(t == 1)
            {
                mysql = "update " + CFields.tb_project_manager + " set "
                        + CFields.project_name + "=? "
                        + CFields.description + "=? "
                        + CFields.user_id + "=? "
                        + CFields.user_ids + "=? "
                        + CFields.user_is_remove + "=? "
                        + CFields.create_date + "=? "
                        + CFields.begin_date + "=? "
                        + CFields.end_date + "=? "
                        + CFields.finish_date + "=? "
                        + CFields.path + "=? "
                        + CFields.isdelete + "=? "
                        + "where " 
                        + CFields.id + "=?";
                cs = config.connec.prepareStatement(mysql);
                cs.setString(1, item.getProject_name());
                cs.setString(2, item.getDescription());
                cs.setLong(3, item.getUser_id());
                cs.setString(4, item.getUser_ids());
                cs.setString(5, item.getUser_is_remove());
                cs.setTimestamp(6,item.getCreate_date());
                cs.setTimestamp(7,item.getBegin_date());
                cs.setTimestamp(8,item.getEnd_date());
                cs.setTimestamp(9,item.getFinish_date());
                cs.setString(10, item.getPaths());
                cs.setBoolean(11, false);
                cs.setLong(12, item.getId());
                result = cs.executeUpdate();
            }
            else
            {
                mysql = "delete from " + CFields.tb_project_manager 
                        + " where "
                        + CFields.id + "=?";
                cs = config.connec.prepareStatement(mysql);
                cs.setLong(1, item.getId());
                result = cs.executeUpdate();
            }
        } 
        catch (SQLException ex) 
        {
            Logger.getLogger(UserBUS.class.getName()).log(Level.SEVERE, null, ex);
        }        
        return result;
    }

    @Override
    public Object insertOnSubmit(Project_manager item, SubConfig config) {
        return this.OnSubmit(item, 0, config);
    }

    @Override
    public Object updateOnSubmint(Project_manager item, SubConfig config) {
        return this.OnSubmit(item,1, config);
    }

    @Override
    public Object deleteOnSubmint(Project_manager item, SubConfig config) {
       return this.OnSubmit(item, 2, config);
    }

    /*
        Minh
        Add Body
    */
    @Override
    protected Project_manager getItem(ResultSet rs) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        Project_manager item = new Project_manager();
        try {
            item.setProject_name(rs.getString(CFields.project_name));
            item.setDescription(rs.getString(CFields.description));
            item.setUser_id(rs.getInt(CFields.user_id));
            item.setUser_ids(rs.getString(CFields.user_ids));
            item.setUser_is_remove(rs.getString(CFields.user_is_remove));
            item.setCreate_date(rs.getTimestamp(CFields.create_date));
            item.setBegin_date(rs.getTimestamp(CFields.begin_date));
            item.setEnd_date(rs.getTimestamp(CFields.end_date));
            item.setFinish_date(rs.getTimestamp(CFields.finish_date));
            item.setPaths(rs.getString(CFields.path));
            item.setId(rs.getLong(CFields.id));
        } 
        catch (SQLException ex) 
        {
            Logger.getLogger(UserBUS.class.getName()).log(Level.SEVERE, null, ex);
        }
        return item;
    }

    @Override
    public List<Project_manager> getAll(SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
